using SqlSugar;
using IM.Easy.Core.Services;
using IM.Easy.Core.Utils;
using IM.Easy.Infrastructure.Query;
using IM.Easy.Core.Extensions;
using IM.Easy.Entity.Msg;
using IM.Easy.MsgServices.IServices;
using IM.Easy.MsgServices.Dtos;
using Mapster;

namespace IM.Easy.MsgServices.Services
{
    public class EmailRecivesService : AppServices<EmailRecives>, IEmailRecivesService
    {

        public EmailRecivesService(ISqlSugarClient db) : base(db)
        {

        }

        public EmailRecives Create(EmailRecivesCreateDto entity)
        {
            var recives = entity.Adapt<EmailRecives>();
            // TODO: 添加创建逻辑
            return Context.Insertable(recives).ExecuteReturnEntity();
        }

        public EmailRecives GetById(long id)
        {
            var email = Context.Queryable<EmailRecives>()
                                .Includes(s => s.Email, e => e.Sender)
                                .InSingle(id);
            return email;
        }

        public PageResponse<EmailRecives> GetPageList(EmailRecivesQueryDto query)
        {
            var queryable = Context.Queryable<EmailRecives>();
            var exp = Expressionable.Create<EmailRecives>();
            exp = exp.AndIF(query.AudienceId != default, s => s.AudienceId == query.AudienceId)
                     .AndIF(!string.IsNullOrEmpty(query.Subject), s => s.Email.Subject.Contains(query.Subject))
                     .AndIF(query.ReadStatus != default, s => s.IsRead == query.ReadStatus);

            var page = queryable
                 .Includes(s => s.Email.ToList(it => new Emails
                 {
                     SenderId = it.SenderId,
                     Subject = it.Subject,
                     Abstract = it.Abstract
                 }), e => e.Sender.ToList(sender => new EmailAccount
                 {
                     Account = sender.Account,
                 }))
                 .Where(exp.ToExpression())
                 .ToPageList<EmailRecives, EmailRecives>(query);
            return page;
        }

        public bool Update(EmailRecives entity)
        {
            // TODO: 添加更新逻辑
            return Context.Updateable(entity).ExecuteCommandHasChange();
        }

        public bool Delete(params long[] Id)
        {
            // TODO: 添加删除逻辑
            return Context.Deleteable<EmailRecives>(Id).ExecuteCommandHasChange();
        }

        /// <summary>
        /// 接收邮箱
        /// </summary>
        /// <param name="emailId"></param>
        /// <param name="userIds"></param>
        /// <returns></returns>
        public bool Recive(long emailId, params long[] userIds)
        {
            var recives = userIds.Select(s => new EmailRecives
            {
                EmailId = emailId,
                AudienceId = s,
                IsRead = false,
                ReciveTime = DateTime.Now,
                IsDeleted = false,
            }).ToList();
            return InsertRange(recives);
        }

        /// <summary>
        /// 设置为已读
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool SetRead(long id)
        {
            var email = GetById(id);
            email.IsRead = true;
            return Update(email);
        }
    }
}
